package com.ruoyi.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.dto.FaultManagementDto;
import com.ruoyi.web.entity.FaultManagement;
import com.ruoyi.web.entity.FaultManagementHandle;
import com.ruoyi.web.service.IFaultManagementHandleService;
import com.ruoyi.web.service.IFaultManagementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/admin/fault")
public class FaultManagementController extends BaseController {

    private final IFaultManagementService faultManagementService;
    private final IFaultManagementHandleService faultManagementHandleService;

    public FaultManagementController(IFaultManagementService faultManagementServiceImpl, IFaultManagementHandleService faultManagementHandleService) {
        this.faultManagementService = faultManagementServiceImpl;
        this.faultManagementHandleService = faultManagementHandleService;
    }


    @PostMapping("/distribution")
    public AjaxResult distribution(@RequestBody FaultManagementHandle faultManagementHandle) {
        FaultManagement dbFaultManagement = faultManagementService.getById(faultManagementHandle.getId());
        BeanUtil.copyProperties(dbFaultManagement, faultManagementHandle);
        faultManagementHandle.setId(IdUtil.getSnowflakeNextId());
        faultManagementHandle.setStatus(1);
        faultManagementHandleService.save(faultManagementHandle);
        dbFaultManagement.setStatus(1);
        faultManagementService.updateById(dbFaultManagement);
        return success();
    }

    /**
     * 查询故障管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultManagement faultManagement) {
        startPage();
        List<FaultManagementDto> list = faultManagementService.selectFaultManagementList(faultManagement);
        return getDataTable(list);
    }

    /**
     * 导出故障管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:export')")
    @Log(title = "故障管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultManagement faultManagement) {
        List<FaultManagementDto> faultManagementDtos = faultManagementService.selectFaultManagementList(faultManagement);
        ArrayList<FaultManagement> faultManagements = new ArrayList<>();
        for (FaultManagementDto faultManagementDto : faultManagementDtos) {
            FaultManagement temp = new FaultManagement();
            BeanUtil.copyProperties(temp, faultManagementDto);
            faultManagements.add(temp);
        }
        ExcelUtil<FaultManagement> util = new ExcelUtil<FaultManagement>(FaultManagement.class);
        util.exportExcel(response, faultManagements, "故障管理数据");
    }

    /**
     * 获取故障管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(faultManagementService.selectFaultManagementById(id));
    }

    /**
     * 新增故障管理
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:add')")
    @Log(title = "故障管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaultManagement faultManagement) {
        LoginUser loginUser = getLoginUser();
        String username = loginUser.getUsername();
        faultManagement.setCreator(username);
        faultManagement.setId(IdUtil.getSnowflakeNextId());
        faultManagement.setStatus(0);
        return toAjax(faultManagementService.insertFaultManagement(faultManagement));
    }

    /**
     * 修改故障管理
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:edit')")
    @Log(title = "故障管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaultManagement faultManagement) {
        return toAjax(faultManagementService.updateFaultManagement(faultManagement));
    }

    /**
     * 删除故障管理
     */
    @PreAuthorize("@ss.hasPermi('admin:fault:remove')")
    @Log(title = "故障管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(faultManagementService.deleteFaultManagementByIds(ids));
    }
}
